<!--
 * @Author: zzh
 * @Date: 2021-04-12 18:00:08
 * @LastEditTime: 2021-04-12 18:52:03
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \pages\login\components\code-test.vue
-->
<template>
  <view>
    <view class="keyboard-show-text" @tap="codeClick">
      <view v-for="(item, index) in codeLength" :key="index" :style="codeSty" class="input-box">
        <view :class="{ 'input-box_active': isActive === index }" />
        {{ codes[index] }}
      </view>
    </view>
    <key-board ref="keyBoard" @del="delNum" @keyinput="keyinput" />
  </view>
</template>

<script>
import KeyBoard from './key-board.vue'

export default {
  components: { KeyBoard },
  props: {
    codeLength: {
      type: Number,
      default: 6
    },
    codeSty: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      codes: [],
      isOpenBoard: false,
      isActive: -1
    }
  },
  methods: {
    delNum(item) {
      this.codes.pop()
      this.isActive = this.codes.length
    },
    keyinput(item) {
      if (this.codes.length < this.codeLength) {
        this.codes.push(item.txt)
      }
      if (this.codes.length === this.codeLength) {
        // 输入长度符合
        this.$refs.keyBoard.close()
        this.$emit('success', this.codes.join(''))
      }
      this.isActive = this.codes.length
    },
    codeClick() {
      if (this.isOpenBoard) {
        this.$refs.keyBoard.close()
      }
      else {
        this.isActive = this.codes.length
        this.$refs.keyBoard.open()
      }
      this.isOpenBoard = !this.isOpenBoard
    }
  }
}
</script>

<style scoped lang="scss">
.keyboard-show-text {
  position: relative;
  display: flex;
  justify-content: space-around;

  .inputcode {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
    color: #fff;
    opacity: 0;
    z-index: 99;
  }

  .input-box {
    position: relative;
    width: 84rpx;
    height: 84rpx;
    // border: 1px solid #747476;
    background: #eee;
    border-radius: 20rpx;
    text-align: center;
    color: #555;
    line-height: 84rpx;
    font-size: 32rpx;

    .input-box_active {
      position: absolute;
      width: 4rpx;
      height: 50rpx;
      top: 20rpx;
      left: 40rpx;
      background: #89bfff;
      animation: cursorImg 1s infinite;
    }
  }
}

@keyframes cursorImg {
  0%,
  100% {
    opacity: 0;
  }

  50% {
    opacity: 1;
  }
}
</style>
